# -*- coding: utf-8 -*-
## zixue@taobao.com
## 2008-6-16

#import urllib
#urlfetch 替换掉 urllib
from google.appengine.api import urlfetch
from Log import Log

class HttpRsp():
	"包含默认为空值的类"
	def __init__(self, statusCode=None, headers=None, content=None):
		self.statusCode = statusCode
		self.content = content
		self.headers = headers

class DefaultHttp():
	"更新的http response 数据"
	def get(self, url):
#		result = urllib.urlopen(url)
		result = urlfetch.fetch(url)
		rsp = HttpRsp()
#		rsp.content = result.read()
		if result.status_code == 200:
			rsp.content = result.content
		return rsp

class HttpClient():
	"Client Http Response 和 DefaultHttp()没有什么区别，为什么要单独写呢？"
	def __init__(self, adapter=DefaultHttp()) :
		self.adapter = adapter

	def get(self, url):
		"""
		get(url) -> HttpResponse
		Do Http Get request and return HttpResponse
		"""
#		Log.debug("req url:" + url)
		rsp = self.adapter.get(url)
#		Log.debug("http rsq\nstatusCode:" + str(rsp.statusCode) + "\ncontent:" + str(rsp.content))
		return rsp

def main():
	Log.enableDebug = True
	client = HttpClient()
	rsp = client.get('http://www.yahoo.com')
	Log.debug('html:\n' + rsp.content)

if __name__ == "__main__":
	main()